//
//  YCLUserAccount.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/12.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

class YCLUserAccount: NSObject ,NSCoding{

    //授权后的 access_token
    var access_token:String?
    
    //授权用户的uid
    var uid:String?
    
    //access_token的生命周期(单位是秒)
    var expires_in:NSTimeInterval = 0 {
    
        didSet{
        
            expiresDate = NSDate.init(timeIntervalSinceNow: expires_in)
        }
    }
    
    //过期时间
    var expiresDate: NSDate?
    
    //用户名
    var screen_name: String?
    
    //用户头像图片
    var avatar_large: String?
    
    
    //字典转模型
    init(dict:[String: AnyObject]) {
        
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //字典中的key在模型中找不到对应的属性时调用
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        YCLPrint("forUndefinedKey:\(key)")
    }
    
    //打印对象
    //只读计算属性
    override var description: String{
    
        get{
        
            return "access_token: \(access_token), uid: \(uid), expires_in: \(expires_in)"
        }
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
}
